#include "createmode.h"
#include "ui_createmode.h"

CreateMode::CreateMode(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CreateMode)
{
    ui->setupUi(this);


    audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    mediaObject = new Phonon::MediaObject(this);
    path = new Phonon::Path;
    path->reconnect(mediaObject, audioOutput);
    mediaObject->setTickInterval(100);

    QLayout *layout;
    volumeSlider = new Phonon::VolumeSlider(audioOutput, ui->volumeWidget);
    layout = ui->volumeWidget->layout();
    layout->addWidget(volumeSlider);
    seekSlider = new Phonon::SeekSlider(mediaObject, ui->progresWidget);
    layout = ui->progresWidget->layout();
    layout->addWidget(seekSlider);

    scene = new MyGraphicsScene;
    ui->graphicsView->setScene(scene);
    ui->graphicsView->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    scene->setTotalTime(300);

    connect(ui->playButton, SIGNAL(clicked()), this, SLOT(play()));
    connect(ui->pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
    connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(stop()));

    connect(mediaObject, SIGNAL(finished()), this, SLOT(finished()));
    connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(newPalayerState(Phonon::State,Phonon::State)));
    connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tiknuti(qint64)));
    connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64)));
    connect(ui->zoomInButton, SIGNAL(clicked()), scene, SLOT(scaleIn()));
    connect(ui->zoomOutButton, SIGNAL(clicked()), scene, SLOT(scaleOut()));

    connect(scene, SIGNAL(mousePressed(MyGraphicsItem*)), this, SLOT(processPressedItem(MyGraphicsItem*)));
    connect(mediaObject, SIGNAL(tick(qint64)), scene, SLOT(tick(qint64)));

    QList<Phonon::AudioOutputDevice> devices = Phonon::BackendCapabilities::availableAudioOutputDevices();
    for(int i = 0; i < devices.size(); i++)
    {
        QString str = devices[i].name();
        ui->audioDeviceCombo->addItem(str);
    }

    connect(ui->applyButton, SIGNAL(clicked()), this, SLOT(changeAudioDevice()));
    connect(ui->AddSong, SIGNAL(clicked()), this, SLOT(loadSong()));

    ui->playButton->setDisabled(true);
    ui->playButton->setIcon(QIcon(":/img/play.png"));
    ui->stopButton->setDefault(true);
    ui->stopButton->setIcon(QIcon(":/img/stop.png"));
    ui->pauseButton->setDisabled(true);
    ui->pauseButton->setIcon(QIcon(":/img/selected.png"));

    listWidget = new MyListWidget(ui->listWidget);
    layout = ui->listWidget->layout();
    layout->addWidget(listWidget);
    MyListWidgetItem *widgetItem = new MyListWidgetItem(new QPixmap(":/img/play_mode.png"), new QColor(Qt::gray), "AD");
    listWidget->addItem(widgetItem);

    scene->addType("AD");
    scene->addType("PO");
    scene->addType("AH");
}

CreateMode::~CreateMode()
{
    delete ui;    

    delete mediaObject;
    delete audioOutput;
    delete path;

    delete volumeSlider;
    delete seekSlider;

    delete scene;
}

void CreateMode::setPort(QtSeriovyPort *port)
{
    this->port = port;
}

void CreateMode::play()
{
    mediaObject->play();
}

void CreateMode::pause()
{
    mediaObject->pause();
}

void CreateMode::stop()
{
    mediaObject->stop();
}

void CreateMode::newPalayerState(Phonon::State newstate, Phonon::State oldstate)
{
    if(newstate == Phonon::PausedState)
    {
        ui->playButton->setDisabled(false);
        ui->playButton->setIcon(QIcon(":/img/play.png"));
        ui->stopButton->setDefault(false);
        ui->stopButton->setIcon(QIcon(":/img/stop.png"));
        ui->pauseButton->setDisabled(true);
        ui->pauseButton->setIcon(QIcon(":/img/pause-selected.png"));
    }
    else if(newstate == Phonon::PlayingState)
    {
        ui->playButton->setDisabled(true);
        ui->playButton->setIcon(QIcon(":/img/play-selected.png"));
        ui->stopButton->setDefault(false);
        ui->stopButton->setIcon(QIcon(":/img/stop.png"));
        ui->pauseButton->setDisabled(false);
        ui->pauseButton->setIcon(QIcon(":/img/pause.png"));
    }
    else if(newstate == Phonon::StoppedState)
    {
        ui->playButton->setDisabled(false);
        ui->playButton->setIcon(QIcon(":/img/play.png"));
        ui->stopButton->setDefault(true);
        ui->stopButton->setIcon(QIcon(":/img/stop-selected.png"));
        ui->pauseButton->setDisabled(true);
        ui->pauseButton->setIcon(QIcon(":/img/pause.png"));
        ui->soundTimeLabel->setText(QString("%1:%2/%3:%4")
                                    .arg(0)
                                    .arg(0)
                                    .arg((mediaObject->totalTime()/60000)%60)
                                    .arg((mediaObject->totalTime()/1000)%60)
                                    );
    }
}

void CreateMode::finished()
{
    mediaObject->setCurrentSource(soundName);
}

void CreateMode::loadSong()
{
    QFileDialog dialog;
    dialog.setNameFilter(tr("Sounds (*.mp3 *.wav *.midi *.ogg *.wma)"));
    dialog.setFileMode(QFileDialog::ExistingFile);
    if(dialog.exec())
    {
        soundName = dialog.selectedFiles().at(0);
        mediaObject->setCurrentSource(soundName);
        ui->playButton->setDisabled(false);
    }
}

void CreateMode::loadTrack()
{
    QFileDialog dialog;
# warning je potreba zmenit track koncovku
    dialog.setNameFilter(tr("Sounds (*.mp3 *.wav *.midi *.ogg *.wma)"));
    dialog.setFileMode(QFileDialog::ExistingFile);
    if(dialog.exec())
    {
        dialog.selectFile(soundName);
        mediaObject->setCurrentSource(soundName);
        ui->playButton->setDisabled(false);
    }
}

void CreateMode::changeAudioDevice()
{
    QList<Phonon::AudioOutputDevice> devices = Phonon::BackendCapabilities::availableAudioOutputDevices();
    audioOutput->setOutputDevice(devices[ui->audioDeviceCombo->currentIndex()]);
}


void CreateMode::tiknuti(qint64 h)
{
    ui->soundTimeLabel->setText(QString("%1:%2/%3:%4")
                                .arg((h/60000)%60)
                                .arg((h/1000)%60)
                                .arg((mediaObject->totalTime()/60000)%60)
                                .arg((mediaObject->totalTime()/1000)%60)
                                );
}

void CreateMode::totalTimeChanged(qint64)
{
    if(scene->totalTime() != mediaObject->totalTime())
    {
        scene->setTotalTime(mediaObject->totalTime());
        /************************************************************************************************/
        /*                          tady asi bude volani nacitani dat pro zobrazeni ve scene            */
        /************************************************************************************************/
    }
}

void CreateMode::processPressedItem(MyGraphicsItem* item)
{
    qDebug()<<"item presed: "<<item->myType();
    // ZPRACOVANI ITEMU V ZAVSLOSTI NA JEJICH TYPU
}
